﻿@using System.Data.SqlClient
@using System.Configuration
@helper Echo(string name) {
    // your helper can contain code and/or markup, e.g.
    var msg = "Hello " + name;
    <p>@msg</p>
    // For more information, visit http://go.microsoft.com/fwlink/?LinkID=204658
}

@helper Asterisk(string color) { <text><sup style="font-weight:bold;color:@color;">*</sup></text> }

@helper LegalName(string identityName)
{
    if (string.IsNullOrEmpty(identityName))
    {
        <text>Anonymous</text>
        return;
    }

    string cacheEntryName = "IntranetId_NameLegal_Dictionary";
    string intranetId = string.Empty;
    string legalName = string.Empty;

    intranetId = KeppelLand.Helpers.TranslationHelper.GetUsernameByDomainName(identityName);

    if (!string.IsNullOrEmpty(intranetId))
    {
        // Grab cache
        Dictionary<string, string> nameDict = HttpContext.Current.Cache.Get(cacheEntryName) as Dictionary<string, string>;
        if (nameDict == null)
        {
            nameDict = new Dictionary<string, string>();
        }
        
        // Check if exists in cache
        if (nameDict.Keys.Contains(intranetId))
        {
            legalName = nameDict[intranetId];
        }
        else
        {
            // get from DB
            using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["EOCS"].ConnectionString))
            {
                System.Data.SqlClient.SqlCommand cmd = conn.CreateCommand();
                cmd.CommandText = "SELECT NameLegal FROM B2E WHERE IntranetID = @IntranetID;";
                cmd.Parameters.AddWithValue("@IntranetID", intranetId);

                conn.Open();
                using (System.Data.SqlClient.SqlDataReader sdr = cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection))
                {
                    if (sdr.Read())
                    {
                        legalName = sdr[0].ToString();
                    }
                }
            }
            
            if (!string.IsNullOrEmpty(legalName))
            {
                nameDict[intranetId] = legalName;
            }
        }

        HttpContext.Current.Cache.Insert(cacheEntryName, nameDict);
    }
    @legalName
}

@helper SectionByEmployeeMaster(string employeeMaster)
{
    if (employeeMaster != null)
    {
        <text>@KeppelLand.Helpers.TranslationHelper.GetSectionByEmployeeMaster(employeeMaster)</text>
    }
}

@helper LegalNameByEmployeeMaster(string employeeMaster)
{
    if (employeeMaster != null)
    {
        <text>@KeppelLand.Helpers.TranslationHelper.GetNameLegalByEmployeeMaster(employeeMaster)</text>
    }
}

@helper FormatDate(object dateObject)
{
    if ((dateObject != null) && (dateObject.GetType() == typeof(System.DateTime)))
    {
        @(((DateTime)dateObject).ToString("dd/MM/yyyy"))
    }
}

@helper FormatDayOfWeek(object dateObject)
{
    if ((dateObject != null) && (dateObject.GetType() == typeof(System.DateTime)))
    {
        @(((DateTime)dateObject).ToString("ddd"))
    }
}

@helper FormatMonth(object dateObject)
{
    // your helper can contain code and/or markup, e.g.
    if ((dateObject != null) && (dateObject.GetType() == typeof(System.DateTime)))
    {
        @(((DateTime)dateObject).ToString("yyyy-MMM"))
    }
}

@helper FormatTime(object timeObject)
{
    // your helper can contain code and/or markup, e.g.
    if ((timeObject != null) && (timeObject.GetType() == typeof(System.DateTime)))
    {
        @(((DateTime)timeObject).ToString("HHmm"))
    }
}

@helper FormatDecimal(object decimalObject)
{
    if ((decimalObject != null) && (decimalObject.GetType() == typeof(System.Decimal)))
    {
        @(((Decimal)decimalObject).ToString("0.##"))
    }
}

@helper FormatMoney(object decimalObject)
{
    if ((decimalObject != null) && (decimalObject.GetType() == typeof(System.Decimal)))
    {
        @(((Decimal)decimalObject).ToString("0.00"))
    }
}


@helper FormatCheckmark(object booleanObject)
{
    if ((booleanObject != null) && (booleanObject.GetType() == typeof(System.Boolean) && (((Boolean)booleanObject) == true)))
    {
        <text><span class="glyphicon glyphicon-ok" aria-hidden="true"></span></text>
    }
}

@helper FormatInteger(object integerObject)
{
    if ((integerObject != null) && (integerObject.GetType() == typeof(System.Int32)))
    {
        @(((int)integerObject).ToString())
    }
}

@helper AddCheckedAttribute(object booleanObject)
{
    if ((booleanObject != null) && (booleanObject.GetType() == typeof(System.Boolean) && (((Boolean)booleanObject) == true)))
    {
        <text>checked="checked"</text>
    }
}

@helper AddSelectedAttribute(object booleanObject)
{
    if ((booleanObject != null) && (booleanObject.GetType() == typeof(System.Boolean) && (((Boolean)booleanObject) == true)))
    {
        <text>selected="selected"</text>
    }
}

@helper AddCheckedAttribute(string booleanString)
{
    bool resultValue = false;
    if (bool.TryParse(booleanString, out resultValue))
    {
        if (resultValue == true)
        {
        <text>checked="checked"</text>
        }

    }
}

@helper AddSelectedAttributeIf(string t1, string t2)
{
    if (t1.Equals(t2, StringComparison.InvariantCultureIgnoreCase))
    {
        <text>selected="selected"</text>
    }
}